#include <stdio.h>
#include "block_queue.h"

struct block_queue {
	block_node *first;
	block_node *last;
	int count;
};

struct block_node {
	block *item;
	struct block_node *next;
};

block_queue *queue_create() {
	block_queue *queue = (block_queue *)malloc(sizeof(block_queue));

	queue->first = NULL;
	queue->last = NULL;
	queue->count = 0;

	return queue;
}

void queue_enqueue(block_queue *queue, block *item) {
	block_node *new_node = (block_node *)malloc(sizeof(block_node));

	new_node->item = item;
	new_node->next = NULL;

	if(queue_empty(queue)) {
		queue->first = new_node;
		queue->last = new_node;
		queue->count++;

		return;
	}

	queue->count++;

	queue->last->next = new_node;
	queue->last = new_node;
}

int queue_empty(block_queue *queue) {
	return queue->count == 0;
}

sector *queue_dequeue(block_queue *queue) {
	block_node *first_node = queue->first;

	queue->first = first_node->next;
	queue->count--;

	return first_node->item;
}

void queue_kill(block_queue *queue) {
	//free(queue);
	queue = NULL;
}

void queue_print(block_queue *queue) {
	block_node *node = queue->first;

	while(node != NULL) {
		printf("Sector: %d | Descriptor: %d | IsConsole: %d \n", node->item->sec, node->item->incoming_desc, node->item->is_console);
		node = node->next;
	}

	printf("Count: %d\n", queue->count);
}
